package hot;

import java.util.Deque;
import java.util.LinkedList;
import java.util.Stack;

/**
 * @author lakersUp
 * @Classname       字符串解码
 * @Description        难点：怎么表示数字
 *                          怎么表示要增倍的字符串
 * @Date 2022/4/1 10:01
 * @Version 1.0
 */
public class Hot394decodeString {
    public String decodeString(String s){
        Deque<Character> stack = new LinkedList<>();
        for(char ch:s.toCharArray()){
            if(ch!=']'){
                stack.push(ch);
            }else {
                StringBuffer sb = new StringBuffer();
                while (!stack.isEmpty() && Character.isLetter(stack.peek())){
                    sb.insert(0,stack.pop());
                }
                String sbL = sb.toString();
                stack.pop();
                sb= new StringBuffer();
                while (!stack.isEmpty() && Character.isDigit(stack.peek())){
                    sb.insert(0,stack.pop());
                }
                int count = Integer.parseInt(sb.toString());
                while (count>0){
                    for(char c:sbL.toCharArray()){
                        stack.push(c);
                    }
                    count--;
                }
            }
        }

        StringBuffer stringBuffer = new StringBuffer();
        while (!stack.isEmpty()){
            stringBuffer.insert(0,stack.pop());
        }
        return stringBuffer.toString();

    }
}
